#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    //my_qthread=new myQthread(this);//挂在对象树上，不考虑资源回收
    my_qthread = new MyQthread;
    connect(my_qthread, &MyQthread::threadSignal, [ = ](int i) {
        ui->lcdNumber->display(QString::number(i));
    });

    //未挂在对象树：关闭窗口，发送destroyed信号
     connect(this,&Widget::destroyed,[=](){
         my_qthread->exit();//从当前线程退出，并返回一个表示状态的整数
         my_qthread->wait();//阻塞主线程，等待调用线程结束
        // my_qthread->deleteLater();//内存泄漏
         delete my_qthread;
     });

   //线程执行结束之后，回收线程
    connect(my_qthread, &QThread::finished, [ = ]() {
        my_qthread->deleteLater();
    });

}
Widget::~Widget()
{
    delete ui;
    // 挂在对象树
    // my_qthread->exit();//从当前线程退出，并返回一个表示状态的整数
    // my_qthread->wait();//阻塞主线程，等待调用线程结束
    qDebug() << "主线程退出";
}

void Widget::on_pushButton_clicked()
{
    my_qthread->start();//调用start内部会自动调用run方法
}
